package org.jeecg.netty.sensor.util;

import java.util.Locale;

/**
 *
 * <p>
 * Description: 十进制转换类
 * </p>
 *
 * @author <a href="mailto: xiebin@54604344@qq.com">xiebin</a>
 * @version $Revision$
 */
public class HexConvert
{

	/**
	 * 16进制表示的字符串转换为字节数组
	 *
	 * @param s
	 *            16进制表示的字符串
	 * @return byte[] 字节数组
	 */
	public static byte[] hexStringToByteArray(String s)
	{
		int len = s.length();
		byte[] b = new byte[len / 2];
		for (int i = 0; i < len; i += 2)
		{
			// 两位一组，表示一个字节,把这样表示的16进制字符串，还原成一个字节
			b[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
		}
		return b;
	}

	/**
	 * 16进制的字符串表示转成字节数组
	 *
	 * @param hexString
	 *            16进制格式的字符串
	 * @return 转换后的字节数组
	 **/
	public static byte[] toByteArray(String hexString)
	{

		hexString = hexString.toLowerCase();
		final byte[] byteArray = new byte[hexString.length() / 2];
		int k = 0;
		for (int i = 0; i < byteArray.length; i++)
		{//因为是16进制，最多只会占用4位，转换成字节需要两个16进制的字符，高位在先
			byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
			byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
			byteArray[i] = (byte) (high << 4 | low);
			k += 2;
		}
		return byteArray;
	}

	/**
	 * byte[]数组转换为16进制的字符串
	 *
	 * @param bytes
	 *            要转换的字节数组
	 * @return 转换后的结果
	 */
	private static String bytesToHexString(byte[] bytes)
	{
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < bytes.length; i++)
		{
			String hex = Integer.toHexString(0xFF & bytes[i]);
			if(hex.length() == 1)
			{
				sb.append('0');
			}
			sb.append(hex);
		}
		return sb.toString();
	}

	/**
	 * byte[]数组转换为16进制的字符串
	 *
	 * @param data
	 *            要转换的字节数组
	 * @return 转换后的结果
	 */
	public static final String byteArrayToHexString(byte[] data)
	{
		StringBuilder sb = new StringBuilder(data.length * 2);
		for (byte b : data)
		{
			int v = b & 0xff;
			if(v < 16)
			{
				sb.append('0');
			}
			sb.append(Integer.toHexString(v));
		}
		return sb.toString().toUpperCase(Locale.getDefault());
	}

	/**
	 * 字节数组转成16进制表示格式的字符串
	 *
	 * @param byteArray
	 *            要转换的字节数组
	 * @return 16进制表示格式的字符串
	 **/
	public static String toHexString(byte[] byteArray)
	{
		if(byteArray == null || byteArray.length < 1)
			throw new IllegalArgumentException("this byteArray must not be null or empty");

		final StringBuilder hexString = new StringBuilder();
		for (int i = 0; i < byteArray.length; i++)
		{
			if((byteArray[i] & 0xff) < 0x10) //0~F前面不零
				hexString.append("0");
			hexString.append(Integer.toHexString(0xFF & byteArray[i]));
		}
		return hexString.toString().toLowerCase();
	}

	/**
	 * 十六进制转成ascii码
	 * <p>
	 * The convertHexToString
	 * </p>
	 *
	 * @param hex
	 * @return
	 * @author:xiebin 2017-3-20
	 * @update: [updatedate] [changer][change description]
	 */
	public static String convertHexToString(String hex)
	{

		StringBuilder sb = new StringBuilder();
		StringBuilder temp = new StringBuilder();

		//49204c6f7665204a617661 split into two characters 49, 20, 4c...
		for (int i = 0; i < hex.length() - 1; i += 2)
		{

			//grab the hex in pairs
			String output = hex.substring(i, (i + 2));
			//convert hex to decimal
			int decimal = Integer.parseInt(output, 16);
			//convert the decimal to character
			if(decimal == 0)
			{
				sb.append(" ");
			}
			else
			{

				sb.append((char) decimal);
			}

			temp.append(decimal);
		}

		return sb.toString();
	}

}
